package com.blog.boot.webapp.controller.common;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.api.system.service.ArticleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by ChenJie on 2018/12/23.
 */
@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {

    @Autowired
    ArticleService articleService;

    /**
     * 这里改field 加精与置顶
     */
    @RequestMapping("/jie-set")
    @ResponseBody
    public RestMsg setArticle( Long id,Integer rank,String field) {
        RestMsg msg = restFactory.getMsg();
        if(SecurityUtils.getSubject().isPermitted("user:artcle")){
            articleService.upArticleByid(id,rank,field);
        }else {
            throw new UnauthorizedException();
        }
        return msg.success("修改成功");
    }

    /**
     * 删除 实际status改为-2
     */
    @RequestMapping("/jie-delete")
    @ResponseBody
    public RestMsg deleteArticle( Long id) {
        RestMsg msg = restFactory.getMsg();
        if(SecurityUtils.getSubject().isPermitted("user:artcle")){
            articleService.upArticleByid(id,Integer.valueOf("-2"),"status");
        }else {
            throw new UnauthorizedException();
        }
        return msg.success("修改成功");
    }
}
